package com.dkcd.chenshiproject.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.wifi.aware.DiscoverySession;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;

import com.dkcd.chenshiproject.MainActivity;
import com.dkcd.chenshiproject.R;
import com.dkcd.chenshiproject.entity.Song;
import com.dkcd.chenshiproject.fragment.IndexMapFragment;
import com.dkcd.chenshiproject.fragment.SendLiveFragment;
import com.dkcd.chenshiproject.service.MusicPlayerService;
import com.dkcd.chenshiproject.tabHostActivity.MusicPavilionFragment;
import com.dkcd.chenshiproject.util.ArtworkUtils;
import com.dkcd.chenshiproject.util.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class IndexActivity extends AppCompatActivity implements View.OnClickListener {
    public static Handler indexHandler;
    private TabHost tabHost;
    private RadioButton rbMap;
    private RadioButton rbSendLive;
    private RadioButton rbMv;
    private RadioButton rbFind;
    private RadioButton rbMe;
    private RadioGroup rgGroup;
    private FrameLayout frameLayout;
    private ImageView playingMusicImage;
    private TextView playingMusicTitle;
    private LinearLayout playingMusicBanner;
    private List<Song> musicData = MainActivity.musicData;
    public static List<Map<String, Object>> list;
    public static IndexActivity indexActivity;
    public static int mIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        init();
        initView();
        initRecommendList();
        setIndexHandler();
    }

    /**
     * @author ChenShi
     * @version：1.0
     * @date 描述: 当activity运行时执行
     */
    @Override
    protected void onStart() {
        super.onStart();
        radioGroupChecked();
    }

    /**
     * @author ChenShi
     * @version：1.0
     * @date 描述: 监听当用户点击 导航栏时发生的操作
     */
    public void radioGroupChecked() {
        rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.rb_map: {
                        //获取Fragment管理器
                        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                        Log.e("导航栏选择了", "onCheckedChanged: 首页");
                        ft.replace(R.id.fl_container, new IndexMapFragment());
                        ft.commit();
                    }
                    break;
                    case R.id.rb_send_live: {
                        Log.e("导航栏选择了", "onCheckedChanged: 直播");
                        //获取Fragment管理器
                        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                        ft.replace(R.id.fl_container, new SendLiveFragment());
                        ft.commit();
                    }
                    break;
                    case R.id.rb_mv: {
                        Log.e("导航栏选择了", "onCheckedChanged: MV");
                    }
                    break;
                    case R.id.rb_find: {
                        Log.e("导航栏选择了", "onCheckedChanged: 社区");
                    }
                    break;
                    case R.id.rb_me: {
                        Log.e("导航栏选择了", "onCheckedChanged: 我的");
                    }
                    break;
                }
            }
        });
    }

    /**
     * @author ChenShi
     * @version：1.0
     * @date 描述: 初始化页面
     */
    private void init() {
        indexActivity = this;
        rbMap = (RadioButton) findViewById(R.id.rb_map);
        rbSendLive = (RadioButton) findViewById(R.id.rb_send_live);
        rbMv = (RadioButton) findViewById(R.id.rb_mv);
        rbFind = (RadioButton) findViewById(R.id.rb_find);
        rbMe = (RadioButton) findViewById(R.id.rb_me);
        rgGroup = (RadioGroup) findViewById(R.id.rg_group);
        frameLayout = (FrameLayout) findViewById(R.id.fl_container);
        playingMusicImage = (ImageView) findViewById(R.id.playing_music_img);
        playingMusicTitle = (TextView) findViewById(R.id.playing_music_title);
        playingMusicBanner = (LinearLayout) findViewById(R.id.playing_music_banner);
    }

    private void initView() {
        //定义底部标签图片大小和位置
        Drawable drawable_news = getResources().getDrawable(R.drawable.selector_tab_mv_rb_choose);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_news.setBounds(0, 0, 120, 120);
        rbMv.setCompoundDrawables(null, drawable_news, null, null);

        //定义底部标签图片大小和位置
        Drawable drawable_news1 = getResources().getDrawable(R.drawable.selector_tab_car_rb_choose);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_news1.setBounds(0, 0, 120, 120);
        rbSendLive.setCompoundDrawables(null, drawable_news1, null, null);

        //定义底部标签图片大小和位置
        Drawable drawable_news2 = getResources().getDrawable(R.drawable.selector_tab_find_rb_choose);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_news2.setBounds(0, 0, 120, 120);
        rbFind.setCompoundDrawables(null, drawable_news2, null, null);

        //定义底部标签图片大小和位置
        Drawable drawable_news3 = getResources().getDrawable(R.drawable.selector_tab_map_rb_choose);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_news3.setBounds(0, 0, 120, 120);
        rbMap.setCompoundDrawables(null, drawable_news3, null, null);

        //定义底部标签图片大小和位置
        Drawable drawable_news4 = getResources().getDrawable(R.drawable.selector_tab_me_rb_choose);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_news4.setBounds(0, 0, 120, 120);
        rbMe.setCompoundDrawables(null, drawable_news4, null, null);

        //获取Fragment管理器
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //设置应用启动 就显示首页
        ft.add(R.id.fl_container, new IndexMapFragment());
        ft.commit();
    }

    /**
     * @author ChenShi
     * @version：1.0
     * @date 描述: 初始化推荐列表
     */
    private void initRecommendList() {
        list = new ArrayList<>();
        if (musicData.size()>30){
            mIndex = new Random().nextInt(musicData.size() - 30) + 1;
        }else {
            //不足30首歌曲
            return;
        }
        System.out.println(mIndex);
        //随机获取30首歌曲
        for (int i = mIndex; i < mIndex + 30; i++) {
            Map<String, Object> map = new HashMap<>();
            Song song = musicData.get(i);
            Bitmap artwork = ArtworkUtils.getArtwork(this, song.song, Long.parseLong(song.songId), Long.parseLong(song.albumId), true);
            map.put("img", artwork);
            map.put("title", song.song);
            list.add(map);
        }
        playingMusicBanner.setOnClickListener(this::onClick);
    }

    /**
     * @author ChenShi
     * @version：1.0
     * @date 描述: message 处理器
     */
    public void setIndexHandler() {
        indexHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                switch (msg.what) {
                    case MusicPlayerService.PLAYING_MUSIC: {
                        Song song = (Song) msg.getData().getSerializable("song");
                        playingMusicImage.setImageBitmap(ArtworkUtils.getArtwork(IndexActivity.this, song.song,
                                Long.parseLong(song.songId), Long.parseLong(song.albumId), true));
                        playingMusicTitle.setText(song.song + "  " + song.singer);

                    }
                    break;
                }
                return false;
            }
        });
    }

    //back 不退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.playing_music_banner: {
                Integer position = (Integer) SPUtils.getValue(this, "position", 0);
                Log.e("点击音乐播放banner", "onClick: " + position);
                Intent intent = new Intent(this, MusicPlayerActivity.class);
                Bundle bundle = new Bundle();
                //传入路径
                bundle.putString("musicPath", musicData.get(position).path);
                bundle.putString("maker", "RecommendFragment");
                //传入id
                bundle.putInt("musicId", position);
                intent.putExtras(bundle);
                startActivity(intent);
            }
            break;
        }
    }
}